package com.alpha;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class CollectFDADrugs {
	public static void main(String args[]) throws IOException {

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://www.accessdata.fda.gov/scripts/cder/drugsatfda/index.cfm?fuseaction=Search.Search_Drug_Name");
		
		Header[] allhs = httpget.getAllHeaders();
		for (Header h : allhs){
			System.out.println(h.getName()+ ":"+h.getValue());	
		}
		HttpResponse response = httpclient.execute(httpget);
		Header[] hs3 = response .getAllHeaders();
		for (Header h : hs3){
			System.out.println(h.getName()+ ":"+h.getValue());	
		}
		
		
		Header[] hs = response.getHeaders("Set-Cookie");
		HttpEntity entity = response.getEntity();

		InputStream instream = entity.getContent();
		int l;
		byte[] tmp = new byte[2048];
 		while ((l = instream.read(tmp)) != -1) {
				
		}
		
		httpget = new HttpGet("http://www.accessdata.fda.gov/scripts/cder/drugsatfda/index.cfm?fuseaction=Search.Overview&DrugName=CABERGOLINE");

 		
 		
		httpget = new HttpGet("http://www.accessdata.fda.gov/scripts/cder/drugsatfda/index.cfm?fuseaction=Search.Overview&DrugName=CABERGOLINE");

		HttpResponse response2 = httpclient.execute(httpget);
		  entity = response2.getEntity();
		if (entity != null) {
			  instream = entity.getContent();
			 tmp = new byte[2048];
			String x = "";
			while ((l = instream.read(tmp)) != -1) {
				x = x + new String (tmp);
				
			}
			x=x.toLowerCase();

			System.out.print(x.indexOf("overview"));
		}
 
	}
}
